package com.hy.item.web;

import com.hy.common.vo.PageResult;
import com.hy.item.pojo.Brand;
import com.hy.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {
    /**
     * 品牌查询
     */
    @Autowired
    private BrandService brandService;
    //接收参数
    // defaultValue = "1",就表示page没有传过来的时候就赋默认值为1
    //required表示是否必须,默认为true
    @RequestMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandPage(
            @RequestParam(value = "page",defaultValue = "1")    Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer row,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc,
            @RequestParam(value = "key",required = false) String key)
    {
        //ok方法中写查询后返回的结果
         ResponseEntity<PageResult<Brand>> entity=ResponseEntity.ok(brandService.queryBrandPage(page,row,sortBy,desc,key));
         return entity;
    }
    /**
     * 新增品牌
     */
    @RequestMapping("add")
    //新增没返回值，所以在ResponseEntity泛型是Void。cid是中间表
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids){

        brandService.saveBrand(brand,cids);
        //没有返回值就写build，有就调用body方法
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 根据分类cid查询品牌
     * @param cid
     * @return
     */
    @RequestMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid){

        return ResponseEntity.ok(brandService.queryBrandByCid(cid));

    }

    /**
     * 根据品牌id查询品牌
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){

        return ResponseEntity.ok(brandService.queryByBrandId(id));

    }

}
